package sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.PreparedStatement;

public class UserDetail {
	
	
	public ArrayList<String>  getdetails() throws SQLException
	{
		String[] st = null;
		ArrayList<String> arr=new ArrayList<String>();
		int i=0;
		try 
		{

			//System.out.println("checking login information");
			//System.out.println("name:"+name +"pass:" +pass);
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			
			ResultSet rs= stmt.executeQuery("SELECT * FROM mtl.users ;");
				while (rs.next())
				{
					arr.add(rs.getString(1));
					arr.add(rs.getString(3));
					arr.add(rs.getString(4));
					arr.add(rs.getString(5));
					arr.add(rs.getString(6));
					arr.add(rs.getString(7));
					
					
				}
				//System.out.print(arr);
			return arr;
		
		}
  
		catch (Exception ex )
		{
				ex.printStackTrace();
		}
		return null;
		
		
	}
	
	
	public int  adduser(ArrayList<String> ar) throws SQLException
	{
		
	
		try 
		{

			
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			System.out.println("g");
			
			stmt.executeUpdate("insert into mtl.users(username,password,first_name,last_name,email,tel,address,permition,connected) values('"+ar.get(0)+"','"+ar.get(1)+"','"+ar.get(2)+"','"+ar.get(3)+"','"+ar.get(4)+"','"+ar.get(5)+"','"+ar.get(6)+"','"+ar.get(7)+"','"+0+"');");
				
				
			return 1;
		
		}
  
		catch (Exception ex )
		{
				ex.printStackTrace();
		}
		return 0;
		
		
	}
	
	public int  deleteuserdb(String st) throws SQLException
	{
		
	
		try 
		{

			
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			System.out.println("deleting");
			
			stmt.executeUpdate("delete from mtl.users where username = '"+st+"';");
				
				
			return 1;
		
		}
  
		catch (Exception ex )
		{
				ex.printStackTrace();
		}
		return 0;
		
		
	}
	
	public int usersDetailsUpdate(ArrayList<String> strupd)
	{
		Statement stmt1;
		
		SQLConnection conn = new  SQLConnection();
		try {
			stmt1 = conn.getConnection().createStatement();
			stmt1.executeUpdate("update  mtl.users set password = '"+strupd.get(2)+"',first_name = '"+strupd.get(1)+"',last_name = '"+strupd.get(3)+"', email = '"+strupd.get(5)+"', address = '"+strupd.get(4)+"', tel = '"+strupd.get(6)+"',permition = '"+strupd.get(7)+"' where username = '"+strupd.get(0)+"';");
			return 1;
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	
	
	
	 public ArrayList<String> getUserDetails(String ud)
	 {
		 ArrayList<String> arr=new ArrayList<String>();
		 Statement stmt;
			SQLConnection conn = new  SQLConnection();
			try {
				stmt = conn.getConnection().createStatement();
				ResultSet rs= stmt.executeQuery("Select * from mtl.users where username = '"+ud+"';");
				while (rs.next())
				{
				arr.add(rs.getString(1));
				arr.add(rs.getString(2));
				arr.add(rs.getString(3));
				arr.add(rs.getString(4));
				arr.add(rs.getString(5));
				arr.add(rs.getString(6));
				arr.add(rs.getString(7));
			}
			return arr;
	 }
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;	 }

}
